﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapQVW_VKPOS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQVW_VKPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryQVW_VKPOS(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapQVW_VKPOS(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertQVW_VKPOS(dwData, 1);
            }
            UpdateQVW_VKPOS(dwLocal);

        }
        public void InsertQVW_VKPOS(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1://20140501
                    or1.CommandText = "INSERT INTO  QVW_VKPOS  "
                        + " SELECT DISTINCT VKPMANDANT AS MANDANT,VKKVB,VKKVG,SUBSTR(VKPNEUDAT,1,4) AS NYEAR,SUBSTR(VKPNEUDAT,5,2) AS NMONTH,SUBSTR(VKPNEUDAT,7,2) AS NDAY,"
                        +"'Q'||CEIL(TO_NUMBER(SUBSTR(VKPNEUDAT,5,2))/3) AS NQUARTER, 'K'||VKPAUFKRS||VKPAUFLFD AS KAUF,'K' AS AUFTYP,'K'||VKPAUFKRS||VKPAUFLFD||VKPAUFPOS AS PAUF ,VKPAUFKRS,VKPAUFLFD,VKPAUFPOS,VKPNEUDAT,VKPNEUBEN,VKPKUNDNR AS KDNR,VKPSACHBE, VKPPOSTYP,VKPARTST AS ARTST,VKPKOLL,VKPART,VKPFARBE,VKPGROE,VKPKOLL||VKPART AS VART,"
                        +"VKPKOLL||VKPART||VKPFARBE AS VFARBE,VKPKOLL||VKPART||VKPFARBE||VKPGROE AS VASKU,VKPPRGR AS PRGR,VKPARTGRP AS ARTGRP,VKPMEEH,VKPPREH,ROUND( VKPPREIS*0.001*VKKKURS*0.0001/(VKPMEEHFKT*0.000001),2)  AS VKPPREIS,"
                        +" PFCOST,ROUND(VKPPREIS*0.001*VKKKURS*0.0001/(VKPMEEHFKT*0.000001)-PFCOST,2) AS GPROFIT,VKKKURS*0.0001 AS EXCHANGERATE,ROUND(VKPMENGE1*0.01*VKPMEEHFKT*0.000001*VKPPREIS*0.001*VKKKURS*0.0001,2)  AS VKPVALUE,"
                        +"ROUND(VKPMENGE1*0.01*VKPMEEHFKT*0.000001,2) AS VKPQTY,ROUND(VKPRESERV*0.01*VKPMEEHFKT*0.000001,2) AS VKPRESERV,ROUND(VKPZUGET*0.01*VKPMEEHFKT*0.000001,2) AS VKPZUGET,ROUND(VKPFREIGE*0.01*VKPMEEHFKT*0.000001,2) AS VKPFREIGE,ROUND(VKPKOMMISS*0.01*VKPMEEHFKT*0.000001,2) AS VKPKOMMISS,"
                        +"ROUND(VKPVERS*0.01*VKPMEEHFKT*0.000001,2) AS VKPVERS,ROUND(VKPFAKT*0.01*VKPMEEHFKT*0.000001,2) AS VKPFAKT,SUBSTR(VKPWUTERM,1,4) AS SYEAR,SUBSTR(VKPWUTERM,5,2) AS SMONTH,SUBSTR(VKPWUTERM,7,2) AS SDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(VKPWUTERM,5,2))/3) AS SQUARTER,"
                        +"SUBSTR(VKPLIEFTERM,1,4) AS DYEAR,SUBSTR(VKPLIEFTERM,5,2) AS DMONTH,SUBSTR(VKPLIEFTERM,7,2) AS DDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(VKPLIEFTERM,5,2))/3) AS DQUARTER,SUBSTR(VKPFREITERM,1,4) AS FYEAR,SUBSTR(VKPFREITERM,5,2) AS FMONTH,"
                        +"SUBSTR(VKPFREITERM,7,2) AS FDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(VKPFREITERM,5,2))/3) AS FQUARTER,SUBSTR(VKPLVDAT,1,4) AS VYEAR,SUBSTR(VKPLVDAT,5,2) AS VMONTH,SUBSTR(VKPLVDAT,7,2) AS VDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(VKPLVDAT,5,2))/3) AS VQUARTER,"
                        + "VKPWVTERM,VKPWUTERM,VKPFREITERM,VKPVERSTERM,VKPLIEFTERM,VKPFRDAT,VKPZKDAT,VKPLVDAT,VKPERL "
                        +" FROM VKPOS JOIN VKKOPF ON VKPMANDANT=VKKMANDANT AND VKPAUFKRS=VKKAUFKRS AND VKPAUFLFD=VKKAUFLFD JOIN QVW_VAFARBEN F ON  VKPKOLL=VAFKOLL AND VKPART=F.VAFART AND VKPFARBE=VAFFARBE AND VKPGROE=VAFPGROE"
                        //+ " WHERE VKPMANDANT=:MANDANT AND VKPNUEDAT>'20140701' AND VKKSA='  'AND  VKPSA='  '   AND VKPMEEHFKT<>'000000000000' ";
                        + " WHERE VKPMANDANT=:MANDANT  AND VKKSA='  'AND  VKPSA='  '   AND VKPMEEHFKT<>'000000000000' ";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public void DeleteQVW_VKPOS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QVW_VKPOS X WHERE X.MANDANT=:MANDANT";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break; 
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryQVW_VKPOS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT M.*,KD.KDSNAME,KDNAME,KDLAND,KDORT FROM QVW_VKPOS M LEFT JOIN QVW_KDSTAMM KD ON KD.KDNR=M.KDNR WHERE MANDANT=:MANDANT"; ;
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "QVW_VKPOS";
            Return = Result;
            return Return;
        }
        public void UpdateQVW_VKPOS(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:

                    break;
            }
            Connect2Proxy(or1);
        }
    }
}
